package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsKeywordMapper;
import com.qf.pojo.DtsIssue;
import com.qf.pojo.DtsKeyword;
import com.qf.service.DtsAdminKeywordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;

@Service
public class DtsAdminKeywordServiceImpl implements DtsAdminKeywordService {

    @Autowired
    DtsKeywordMapper keywordMapper;

    @Override
    public IPage<DtsKeyword> queryPage(Integer page, Integer limit, String sort, String order, String keyword, String url) {
        IPage<DtsKeyword> iPage = new Page<>(page,limit);
        QueryWrapper<DtsKeyword> qw = new QueryWrapper<>();
        if(!StringUtils.isEmpty(keyword)){
            qw.like("`keyword`",keyword);
        }
        if(!StringUtils.isEmpty(url)){
            qw.eq("`url`",url);
        }
        qw.eq("deleted",false);
        if(!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)){
            if(order.equalsIgnoreCase("desc")){
                qw.orderByDesc(sort);
            }else {
                qw.orderByAsc(sort);
            }
        }
        keywordMapper.selectPage(iPage,qw);
        return iPage;
    }

    @Override
    public DtsKeyword insert(DtsKeyword keyword) {
        keyword.setAddTime(new Date());
        keyword.setDeleted(false);
        keyword.setUpdateTime(new Date());
        keywordMapper.insert(keyword);
        return keyword;
    }

    @Override
    public void update(DtsKeyword keyword) {
        keyword.setUpdateTime(new Date());
        keyword.setDeleted(false);
        keywordMapper.updateById(keyword);
    }

    @Override
    public void delete(DtsKeyword keyword) {
        keyword.setDeleted(true);
        keywordMapper.updateById(keyword);
    }
}
